#include "mainwindowimpl.h"

#define EM_LS GSL_CONST_MKSA_SPEED_OF_LIGHT //2.99792458e8		//скорость света

void MainWindowImpl::on_but_diag_clicked()
{
    isSec=0;
    QString str,sig,sti;
    int pr,NPt,sopr=1;	//NPt: number of points through teta; sopr: sign of image patr
    double ampl,arg;    //control of sopr-change
    double param0,paramN, dPar;
    Integrals *toFt;
    __complex__ double EIt; //complex coordinates of tensity vector
    gsl_complex EItog[3], vectorE; 	//complex coordinates of tensity vector
    toFt=&ft;
    initCalc(); //Initialization
    on_but_printParam_clicked(); //print
    NPt=boxNdiag->value();
    progress_int->setMaximum(NPt);
    //begin of evaluating
    param0=boxParam0->value();
    paramN=boxParamN->value();
    switch (param)
    {
    case 0 :    //q1 ~ Distance,R
        par=q1;
        param0=param0*ft.b;
        paramN=paramN*ft.b;
        break;
    case 1 :    //q2 ~ Teta, Fi
        par=q2;
        param0=param0/180*M_PI;
        paramN=paramN/180*M_PI;
        break;
    case 2 :    //q3 ~ Fi, Distance
        par=q3;
        if(method==0 || method==1 || method==3)
        {
            param0=param0/180*M_PI;
            paramN=paramN/180*M_PI;
        }
        break;
    }
    dPar=(paramN-param0)/NPt;
    int j=0;
    __complex__ double bufRES[9];
    double ReE, ImE;
    double Norm=1,signImag,lastSign=0;
    for (*par = param0 ; *par < (paramN+dPar) ; *par += dPar)
    {
        progress_int->setValue(j);	// progress-bar
        waveSqlModel->insertRows(j,1);	//inserting row in the database
        GSL_SET_COMPLEX(&vectorE,0,0); ReE=0; ImE=0;
        if(area==3)
        {/*! tab both areas */
            ft.calc(1,method);
            for (int i=0; i<9; i++) bufRES[i]=ft.RES[i];
            ft.calc(2,method);
            for (int i=0; i<9; i++) bufRES[i]+=ft.RES[i];
            for (int i=0; i<3; i++)
            {
                EIt=bufRES[i]+bufRES[i+3]+bufRES[i+6];
                GSL_SET_COMPLEX(&EItog[i],__real__(EIt),__imag__(EIt));
            }
        }
        else
        {/*! This is a check then area=0 or a tab of single area */
            ft.calc(area,method);
            for (int i=0; i<3; i++)
            {
                EIt=ft.RES[i]+ft.RES[i+3]+ft.RES[i+6];
                GSL_SET_COMPLEX(&EItog[i],__real__(EIt),__imag__(EIt));
            }
        }
        /*! new vector detection */
        for (int i=0;i<3;i++)
        {
            ReE+=pow(GSL_REAL(EItog[i]),2);
            ImE+=pow(GSL_IMAG(EItog[i]),2);
        }
        GSL_SET_COMPLEX(&vectorE,sqrt(ReE),sqrt(ImE));
        ampl=gsl_complex_abs(vectorE);	//Amplitude
        if(j==0) Norm=ampl;
        waveSqlModel->setData(waveSqlModel->index(j,32),ampl);   //Amplitude without norming
        signImag=GSL_IMAG(vectorE);	//Argument
        if(lastSign*signImag<0 && signImag>0) sopr*=1; //! correcting disabled
        lastSign=signImag;
        vectorE=gsl_complex_mul_real(vectorE,sopr/Norm);    // Norming
        arg=gsl_complex_arg(vectorE);
        waveSqlModel->setData(waveSqlModel->index(j,33),arg);	//argument after correcting
        /*! filling Database new Row */
        waveSqlModel->setData(waveSqlModel->index(j,1),GSL_REAL(vectorE));
        waveSqlModel->setData(waveSqlModel->index(j,2),GSL_IMAG(vectorE));
        for(pr=0;pr<9;pr++) //grad_div, rot and j
        {
            waveSqlModel->setData(waveSqlModel->index(j,2*pr+12),__real__(ft.RES[pr]));//writing of real component
            waveSqlModel->setData(waveSqlModel->index(j,2*pr+13),__imag__(ft.RES[pr]));//writing of imag component
        }
        for (pr=0;pr<3;pr++)	//pr - number of coordinate; 0:R, 1: teta, 2: fi
        {
            waveSqlModel->setData(waveSqlModel->index(j,2*pr+3),GSL_REAL(EItog[pr]));	//writing of real component
            waveSqlModel->setData(waveSqlModel->index(j,2*pr+4),GSL_IMAG(EItog[pr]));	//writing of imag component
        }
        waveSqlModel->setData(waveSqlModel->index(j,9),*q1);
        waveSqlModel->setData(waveSqlModel->index(j,10),*q2/M_PI*180);	//	put Fi,Z for Cil
        waveSqlModel->setData(waveSqlModel->index(j,11),(method==2 || method==4)?(*q3):(*q3/M_PI*180));	//and Teta,Fi for Scope
        waveSqlModel->setData(waveSqlModel->index(j,30),area);
        waveSqlModel->setData(waveSqlModel->index(j,31),method);
        waveSqlModel->submitAll();	//writing data into database
        j++;
    }
    //end of evaluating
    waveTable->resizeColumnsToContents();
}

void MainWindowImpl::on_but_diasec_clicked()
{
    isSec=1;
    QString str,sig,sti;
    //        if (param!=0) QMessageBox::information(0, qApp->tr("Unrecognize option"),
    //                                               qApp->tr("Please select the R as a parameter\n"
    //                                                        "for this calculation"),
    //                                               QMessageBox::Close);
    int pr,NPt,sopr=1;	//NPt: number of points through teta; sopr: sign of image patr
    double ampl,arg;    //control of sopr-change
    double param0,paramN, dPar;
    Integrals *toFt;
    __complex__ double EIt; //complex coordinates of tensity vector
    gsl_complex EItog[3], vectorE; 	//complex coordinates of tensity vector
    toFt=&ft;
    initCalc(); //Initialization
    on_but_printParam_clicked(); //print
    NPt=boxNdiag->value();
    progress_int->setMaximum(NPt*NPt);
    //begin of evaluating
    param0=boxParam0->value();
    paramN=boxParamN->value();
    param0=param0*ft.b;
    paramN=paramN*ft.b;
    dPar=(paramN-param0)/NPt;
    int j=0;
    __complex__ double bufRES[9];
    gsl_complex stage;
    double Norm=1,signImag,lastSign=0;
    for (int m=0 ; m < (NPt+1) ; m++)
    {
        for (int n=0 ; n < (NPt+1) ; n++)
        {
            *q1=sqrt(pow(m*dPar,2)+pow(n*dPar,2));
            GSL_SET_COMPLEX(&stage,n,m);
            *q2=gsl_complex_arg(stage);
            progress_int->setValue(j);	// progress-bar
            waveSqlModel->insertRows(j,1);	//inserting row in the database
            GSL_SET_COMPLEX(&vectorE,0,0);
            if(area==3)
            {/*! tab both areas */
                ft.calc(1,method);
                for (int i=0; i<9; i++) bufRES[i]=ft.RES[i];
                ft.calc(2,method);
                for (int i=0; i<9; i++) bufRES[i]+=ft.RES[i];
                for (int i=0; i<3; i++)
                {
                    EIt=bufRES[i]+bufRES[i+3]+bufRES[i+6];
                    GSL_SET_COMPLEX(&EItog[i],__real__(EIt),__imag__(EIt));
                }
            }
            else
            {/*! This is a check then area=0 or a tab of single area */
                ft.calc(area,method);
                for (int i=0; i<3; i++)
                {
                    EIt=ft.RES[i]+ft.RES[i+3]+ft.RES[i+6];
                    GSL_SET_COMPLEX(&EItog[i],__real__(EIt),__imag__(EIt));
                }
            }
            /*! absolute value & argument of complex vector */
            for (int i=0;i<3;i++) vectorE=gsl_complex_add(vectorE,gsl_complex_pow_real(EItog[i],2));
            vectorE=gsl_complex_sqrt(vectorE);
            ampl=gsl_complex_abs(vectorE);	//Amplitude
            if(j==0) Norm=ampl;
            waveSqlModel->setData(waveSqlModel->index(j,32),ampl/Norm);   //Amplitude after norming
            signImag=GSL_IMAG(vectorE);	//Argument
            if(lastSign*signImag<0 && signImag>0) sopr*=-1;
            lastSign=signImag;
            vectorE=gsl_complex_mul_real(vectorE,sopr/Norm);
            arg=gsl_complex_arg(vectorE);
            waveSqlModel->setData(waveSqlModel->index(j,33),arg);	//argument after correcting
            /*! filling Database new Row */
            waveSqlModel->setData(waveSqlModel->index(j,1),GSL_REAL(vectorE));
            waveSqlModel->setData(waveSqlModel->index(j,2),GSL_IMAG(vectorE));
            for(pr=0;pr<9;pr++) //grad_div, rot and j
            {
                waveSqlModel->setData(waveSqlModel->index(j,2*pr+12),__real__(ft.RES[pr]));//writing of real component
                waveSqlModel->setData(waveSqlModel->index(j,2*pr+13),__imag__(ft.RES[pr]));//writing of imag component
            }
            for (pr=0;pr<3;pr++)	//pr - number of coordinate; 0:R, 1: teta, 2: fi
            {
                waveSqlModel->setData(waveSqlModel->index(j,2*pr+3),GSL_REAL(EItog[pr]));	//writing of real component
                waveSqlModel->setData(waveSqlModel->index(j,2*pr+4),GSL_IMAG(EItog[pr]));	//writing of imag component
            }
            waveSqlModel->setData(waveSqlModel->index(j,9),*q1);
            waveSqlModel->setData(waveSqlModel->index(j,10),*q2/M_PI*180);
            waveSqlModel->setData(waveSqlModel->index(j,11),*q3);
            waveSqlModel->setData(waveSqlModel->index(j,30),area);
            waveSqlModel->setData(waveSqlModel->index(j,31),method);
            waveSqlModel->submitAll();	//writing data into database
            j++;
        }
    }
    //end of evaluating
    waveTable->resizeColumnsToContents();
}

void MainWindowImpl::on_but_printParam_clicked()
{
    QString str,sig;
    str= "Input parameters of Diagram:\n";
    str+="Frequency:\t"+sig.setNum(ft.f0)+"\trad/c\n";
    str+="a: \t"+sig.setNum(ft.a)+"\tm\n";
    str+="b: \t"+sig.setNum(ft.b)+"\tm\n";
    str+="k: \t"+sig.setNum(ft.k)+"\tm\n";
    str+="Distantion: \t"+sig.setNum(ft.Dist)+"\tm\n";
    str+="alfa1:\t"+sig.setNum(ft.alfa1)+"\t1/m\n";
    str+="alfa2: i*\t"+sig.setNum(__imag__(ft.alfa2))+"\t1/m\n";
    str+="beta: \t"+sig.setNum(ft.beta)+"\t1/m\n";
    text_cp->append(str);
    ft.textParam=str;
}

